import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;


//这个库是用来存储具体的学生、老师信息的
public class UserInfArray {
    ArrayList<Student> studentArrayList = new ArrayList<Student>();
    ArrayList<Teacher> teacherArrayList = new ArrayList<Teacher>();
//    ArrayList<Manager> managerArrayList = new ArrayList<Manager>();

    //新增学生信息
    public void singleStudentInformationEntry(Student newStudentInf)
    {
        studentArrayList.add(newStudentInf);
    }
    //新增教师信息
    public void singleTeacherInformationEntry(Teacher newTeacherInf)
    {
        teacherArrayList.add(newTeacherInf);
    }

    public  void userInfArrayInit()
    {
        String studentArrayList = "src/studentArrayList";
        String teacherArrayList = "src/teacherArrayList";

        studentInformationInit (studentArrayList);
        teacherInformationInit(teacherArrayList);
    }
    //学生信息库录入
    public void studentInformationInit(String filename)
    {
        try {
            // 读取文件内容到字符串列表
            List<String> lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8);

            // 遍历字符串列表并输出每一行
            for (String line : lines) {
                // 使用空格分隔每行数据
                String[] split = line.split("\\s+");
                if (split.length >= 6) {
                    Student newstudent = new Student();
                    newstudent.setSID(split[0]);
                    newstudent.setName(split[1]);
                    newstudent.setGender(split[2]);
                    newstudent.setBirth(split[3]);
                    newstudent.setCollege(split[4]);
                    newstudent.setMajor(split[5]);
                    studentArrayList.add(newstudent);
                    // 其他操作
                } else {
                    // 处理不符合格式的行
                    continue;
                }
            }
        } catch (IOException e) {
            // 处理异常

        }
    }

    //教师信息库录入
    public void teacherInformationInit(String filename)
    {
        try {
            // 读取文件内容到字符串列表
            List<String> lines = Files.readAllLines(Paths.get(filename), StandardCharsets.UTF_8);

            // 遍历字符串列表并输出每一行
            for (String line : lines) {
                // 使用空格分隔每行数据
                String[] split = line.split("\\s+");
                if (split.length >= 6) {
                    Teacher newteacher = new Teacher();
                    newteacher.setTID(split[0]);
                    newteacher.setName(split[1]);
                    newteacher.setGender(split[2]);
                    newteacher.setBirth(split[3]);
                    newteacher.setCollege(split[4]);
                    newteacher.setMajor(split[5]);
                    teacherArrayList.add(newteacher);
                    // 其他操作
                } else {
                    // 处理不符合格式的行
                    continue;
                }
            }
        } catch (IOException e) {
            // 处理异常

        }
    }

    //学生个人信息查询
    public Student studentInformationQuery(String sID)
    {
        for (Student student : studentArrayList) {
            if (student.getSID().equals(sID)) {
                return student;
            }
        }
        return null; // 如果未找到对应学号的学生，则返回null
    }

    //教师个人信息查询
    public Teacher teacherInformationQuery(String tID)
    {
        for (Teacher teacher : teacherArrayList) {
            if (teacher.getTID().equals(tID)) {
                return teacher;
            }
        }
        return null; // 如果未找到对应学号的学生，则返回null
    }

    public void studentInfArrayUpdate(String filePath,  ArrayList<Student> ArrayList) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            // 遍历HashMap并将键值对写入文件
            for (Student student : ArrayList) {
                writer.write(student.getName() + " ");
                writer.write(student.getSID() + " ");
                writer.write(student.getGender() + " ");
                writer.write(student.getBirth() + " ");
                writer.write(student.getCollege() + " ");
                writer.write(student.getMajor() );
            }
//            System.out.println("ArrayList contents have been written to the file.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void teacherInfArrayUpdate(String filePath,  ArrayList<Teacher> ArrayList) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            // 遍历HashMap并将键值对写入文件
            for (Teacher teacher : ArrayList) {
                writer.write(teacher.getName() + " ");
                writer.write(teacher.getTID() + " ");
                writer.write(teacher.getGender() + " ");
                writer.write(teacher.getBirth() + " ");
                writer.write(teacher.getCollege() + " ");
                writer.write(teacher.getMajor() );
            }
//            System.out.println("ArrayList contents have been written to the file.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void userInfArrayUpdateAll()
    {
        String teacherArrayFile = "src/teacherArrayList";
        String studentArrayFile = "src/studentArrayList";

        studentInfArrayUpdate(studentArrayFile,studentArrayList);
        teacherInfArrayUpdate(teacherArrayFile,teacherArrayList);

    }

}
